package com.beijiteshop.shop.ui.category.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.beijiteshop.libcommon.base.adapter.showEmpty
import com.beijiteshop.libcommon.base.adapter.showError
import com.beijiteshop.libcommon.base.adapter.showLoading
import com.beijiteshop.libcommon.base.adapter.showNetError
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentCategoryListBinding
import com.beijiteshop.shop.model.api.response.CategoryBean
import com.beijiteshop.shop.model.entity.ProductItemEntity
import com.beijiteshop.shop.ui.category.vm.CategoryVM
import com.beijiteshop.shop.ui.detail.ProductDetailFragment
import com.beijiteshop.shop.ui.home.adapter.ProductAdapter
import com.beijiteshop.shop.utils.HomeGridItemDecoration
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/10 19:37
 * @Description
 */
class CategoryListFragment : BaseContainerFragment(R.layout.fragment_category_list) {

    companion object{

        const val BUNDLE_CATEGORY = "bundle_category"

        private const val INIT_PAGE = 1

        fun newInstance(categoryBean: CategoryBean): CategoryListFragment{
            return CategoryListFragment().apply {
                val bundle = Bundle().apply {
                    putParcelable(BUNDLE_CATEGORY,categoryBean)
                }
                arguments = bundle
            }
        }
    }

    private val mCategoryVM: CategoryVM by viewModel()
    private val mBinding by bindView<FragmentCategoryListBinding>()
    private val mAdapter = ProductAdapter(mutableListOf())
    private var mCategoryBean: CategoryBean? = null
    private var mPage = INIT_PAGE

    override fun onLazyLoad() {
        initList()
    }

    override fun initView(view: View) {
        mCategoryBean = arguments?.getParcelable(BUNDLE_CATEGORY)

        initRV()
    }

    override fun initData() {

    }

    override fun initEvent() {
        startObserve()
    }

    private fun initList(){
        mCategoryBean?.let {
            mAdapter.showLoading()
            mCategoryVM.getCategoryProducts(mPage,it.bus_GoodTypeID)
        }
    }

    private fun requestData(){
        mCategoryBean?.let {
            mCategoryVM.getCategoryProducts(mPage,it.bus_GoodTypeID)
        }
    }

    private fun initRV(){
        mAdapter.apply {

            loadMoreModule.setOnLoadMoreListener {
                mPage++
                requestData()
            }

            setGridSpanSizeLookup { _, _, position ->
                return@setGridSpanSizeLookup mAdapter.data[position].spanSize
            }

            setOnItemClickListener { _, _, position ->
                val bundle = Bundle()
                bundle.putParcelable(ProductDetailFragment.BUNDLE_PRODUCT,mAdapter.data[position].data)
                startActivity(PageIndex.PRODUCT_DETAIL,bundle)
            }
        }

        mBinding.categoryListRv.apply {
            addItemDecoration(HomeGridItemDecoration(10))
            layoutManager = GridLayoutManager(requireContext(),2)
            adapter = mAdapter
        }
    }

    private fun startObserve(){
        mCategoryVM.apply {
            liveCategoryProducts.observeKt {data ->
                if (mPage == INIT_PAGE){
                    mAdapter.setNewInstance(data)
                }else{
                    updateRVState(false,data)
                }
            }

            liveEmpty.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showEmpty(it){
                        initList()
                    }
                }else{
                    updateRVState(false,null)
                }
            }

            liveError.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveNetError.observeKt {
                if (mPage == INIT_PAGE){
                    mAdapter.showNetError(it){
                        initList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }
        }
    }

    /**
     * 更新列表加载状态
     */
    private fun updateRVState(error: Boolean,data: MutableList<ProductItemEntity>?){
        if (error){
            if (mAdapter.loadMoreModule.isLoading){
                mAdapter.loadMoreModule.loadMoreFail()
            }
        }else{
            if (mAdapter.loadMoreModule.isLoading){
                if (data.isNullOrEmpty()){
                    mAdapter.loadMoreModule.loadMoreEnd()
                }else{
                    mAdapter.addData(data)
                    mAdapter.loadMoreModule.loadMoreComplete()
                }
            }
        }
    }
}